package com.csun.nusing.ui.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.UserInfoJsonBean;
import com.csun.nusing.bean.jsonbean.VersionJsonBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.VersionUpdateService;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.ToastUtil;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import cn.jiguang.net.HttpResponse;

/**
 * 作者: Created by fzq on 2018/2/11 13:38
 * 邮箱: 15997123593@163.com
 */
public class VersionNumberActivity  extends BaseActivity<CommonPresenter> {


    private TextView versioncodetv;
    private RelativeLayout toprl;
    private ImageView appiv;
    private boolean isFamilyFlag;
    private ImageView sharetv;
    private TextView newtv;
    private String authorization;
    private String newversionCode;
    private ProgressDialog m_progressDlg;
    private String appName = "nusing.apk";// app名称
    private String upUrl = "http://scun-yun-oos1.oss-cn-shanghai.aliyuncs.com/dryer/exe/20180206104407296/nusing-debug.apk";

    @Override
    public int getContentViewId() {
        return R.layout.activity_version_number;
    }

    @Override
    public void initSubViews(View view) {
        super.initSubViews(view);
        versioncodetv = (TextView) findViewById(R.id.version_number_versioncode_tv);
        toprl = (RelativeLayout)findViewById(R.id.version_number_top_rl);
        appiv = (ImageView) findViewById(R.id.version_number_app_iv);
        sharetv = (ImageView)findViewById(R.id.version_share_iv);
        newtv = (TextView)findViewById(R.id.version_number_new_tv);
        versioncodetv.setText(getString(R.string.version)+"版");
        sharetv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent textIntent = new Intent(Intent.ACTION_SEND);
                textIntent.setType("text/plain");
                textIntent.putExtra(Intent.EXTRA_TEXT, "App下载地址:http://www.51cs8.com/");
                startActivity(Intent.createChooser(textIntent, "分享"));
            }
        });
        newtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchNewVersion();
            }
        });
    }

    /**
     * 检查新版本号
     */
    private void SearchNewVersion() {
        if (newversionCode!=null){
            if (newversionCode!=getString(R.string.version)){
                showUpDateDialog();
            }else{
                ToastUtil.showMessage(this,getString(R.string.is_new_version));
            }
        }
    }

    /**
     * 弹出提醒更新Dialog
     */
    private void showUpDateDialog() {
         /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(VersionNumberActivity.this);
        normalDialog.setIcon(R.drawable.app_logo);
        normalDialog.setTitle("检查新版本");
        normalDialog.setMessage("是否下载新版本?");
        normalDialog.setPositiveButton("是",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        showDownDialog();
                    }
                });
        normalDialog.setNegativeButton("否",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }

    /**
     * 下载进度条
     */
    private void showDownDialog() {
        VersionUpdateService.beginUpdate(this,"下载",upUrl,R.drawable.app_logo,GuidActivity.class);
    }



    @Override
    public void initData() {
        super.initData();
        isFamilyFlag = getIntent().getBooleanExtra("isFamily",false);
        if (isFamilyFlag){
            toprl.setBackgroundResource(R.drawable.main_whiteyello_color_shape);
            appiv.setImageResource(R.drawable.app_logojiashu);
        }else{
            toprl.setBackgroundResource(R.drawable.main_whiteblur_color_shape);
            appiv.setImageResource(R.drawable.app_logo);
        }
        Map<String, String> param = new HashMap<String, String>();
        authorization = (String) SPUtils
                .get(this, "authorization", "");
        OkHttpTask.newInstance(this).post(param, OkHttpTask.OkHttpTaskBuidler.OkType.form)
                .addHeader("authorization", authorization)
                .enqueue(new IOkTaskCallback() {
                    @Override
                    public void onSuccess(String result) {
                        if (result != null) {
                            Gson gson = new Gson();
                            VersionJsonBean versionbean = gson.fromJson(result,VersionJsonBean.class);
                            if (versionbean.getData()!=null&&versionbean.getData().size()!=0) {
                                newversionCode = versionbean.getData().get(0).getVersionid();
                            }
                        }
                    }
                }).start(UrlBean.VERSIONSHOWURL);
    }
}
